package com.banshuibao.bsb_android.activity;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.CommitAnswerModel;
import com.banshuibao.bsb_android.util.CameraUtil;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.MyEditText;
import com.banshuibao.bsb_android.util.XToast;

import java.util.List;

public class QuestionCloselyActivity extends BaseActivity implements View.OnClickListener, CameraUtil.OnClickOkListener {

    private String uuid;
    private double price;
    private String tag;
    private MyEditText editText;
    private RecyclerView recyclerView;
    private TextView image_count;
    private CameraUtil cameraUtil;
    private String[] images;
    private RelativeLayout relativeLayout;
    //使用广播传图片
    LocalBroadcastManager localBroadcastManager;
    BroadcastReceiver bReceiver;
    IntentFilter iFilter;
    private ImageView title;;
    private TextView  msg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question_closely);
        Intent intent = getIntent();
        uuid = intent.getStringExtra("uuid");
        tag = intent.getStringExtra("tag");
        price = intent.getDoubleExtra("price", 0);
        initView();
    }

    public void initView() {
        TextView send = findViewById(R.id.menu);
        send.setVisibility(View.VISIBLE);
        send.setText("发布");
        editText = findViewById(R.id.myEdit);
        image_count = findViewById(R.id.pic_count);
        recyclerView = findViewById(R.id.recyclerView);
        relativeLayout = findViewById(R.id.relative4);
        title = findViewById(R.id.tag);
        msg = findViewById(R.id.msg);
        cameraUtil = new CameraUtil(this, 3);
        cameraUtil.setLayout(recyclerView);
        cameraUtil.setOnClickOkListener(this);
        send.setOnClickListener(this);
        if (TextUtils.equals("o2o", tag)) {
            setTitleText("一对一咨询");
            // 新建一个可以添加属性的文本对象
            SpannableString ss = new SpannableString("请详细描述您的问题，可附上图片...");
            // 新建一个属性对象,设置文字的大小
            AbsoluteSizeSpan ass = new AbsoluteSizeSpan(13, true);
            // 附加属性到文本
            ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            editText.setHint(new SpannableString(ss));
            title.setBackground(getResources().getDrawable(R.drawable.o2o_title));
            msg.setText(R.string.question_msg);
            iFilter = new IntentFilter();
            iFilter.addAction("O2oAction");
            iFilter.setPriority(100);
            bReceiver = new PayActivity.shangBroadCast();
            localBroadcastManager = LocalBroadcastManager.getInstance(this);
            localBroadcastManager.registerReceiver(bReceiver, iFilter);
        } else if (TextUtils.equals("expert", tag)) {
            setTitleText("追问");
            // 新建一个可以添加属性的文本对象
            SpannableString ss = new SpannableString("请输入您要追问的内容......");
            // 新建一个属性对象,设置文字的大小
            AbsoluteSizeSpan ass = new AbsoluteSizeSpan(13, true);
            // 附加属性到文本
            ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            editText.setHint(new SpannableString(ss));
            editText.setTextColor(getResources().getColor(R.color.text_black));
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 316);
            layoutParams.setMargins(30, 30, 30, 0);
            editText.setLayoutParams(layoutParams);
            editText.setBackground(getResources().getDrawable(R.drawable.edit_shape));
            relativeLayout.setVisibility(View.GONE);
            recyclerView.setVisibility(View.GONE);
            title.setVisibility(View.GONE);

            msg.setTextColor(getResources().getColor(R.color.append_color));
            msg.setAlpha(0.7f);
            msg.setText("提示：您只有2次追问机会");
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.menu:
                final String content = editText.getText().toString().trim();
                if (TextUtils.isEmpty(content)) {
                    XToast.showToast(QuestionCloselyActivity.this, "请输入咨询内容");
                    return;
                }
                if (TextUtils.equals("o2o", tag)) {
                    showDialog(content, "O2oAction", 2);
                } else if (TextUtils.equals("expert", tag)) {
                    showDialog(content, "CloselyAction", 3);
                }

                break;
        }
    }

    @Override
    public void gobackData(List<String> list) {
        if (list != null) {
            image_count.setText("(" + list.size() + "/3)");
            images = new String[list.size()];
            for (int i = 0; i < list.size(); i++) {
                images[i] = list.get(i);
            }
        }
    }

    /**
     * 弹窗
     */
    public void showDialog(final String content, final String action, final int flag) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this).setCancelable(false);  //先得到构造器
        builder.setTitle("确认发布"); //设置标题
        builder.setMessage("发布的问题请在 我的 -> 咨询清单 中查看"); //设置内容
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { //设置确定按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (images != null && images.length > 0) {
                    Intent intent = new Intent();
                    intent.setAction(action);
                    intent.putExtra("images", images);
                    localBroadcastManager.sendBroadcastSync(intent);
                }
                if (flag == 2) {
                    IntentManager.goPayResultActivity(QuestionCloselyActivity.this, content, String.valueOf(price), uuid, 2);
                } else if (flag == 3) {
                    setPost(content);
//                    IntentManager.goPayResultActivity(QuestionCloselyActivity.this,content,String.valueOf(price),uuid,3);
                }
                dialog.dismiss();

            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { //设置取消按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        //参数都设置完成了，创建并显示出来
        builder.create().show();
    }

    private void setPost(String content) {
        CommitAnswerModel model = new CommitAnswerModel();
        if (!TextUtils.isEmpty(uuid)) {
            model.setUuid(uuid);
        }
        if (!TextUtils.isEmpty(content)) {
            model.setContent(content);
        }
        HttpUtil.doPost(Constants.Url.ONE_ASK, model, new HttpResponse(this) {
            @Override
            public void onResult(Object obj) {
//                String s1 = (String) obj;
//                LogUtil.e(s1);
                XToast.showToast(QuestionCloselyActivity.this, "发布成功，请耐心等待回答");
                IntentManager.goTabActivity(QuestionCloselyActivity.this);
                finish();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (cameraUtil != null) {
                cameraUtil.clear();
            }
            editText.setText("");
            finish();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (localBroadcastManager != null) {
            localBroadcastManager.unregisterReceiver(bReceiver);
        }

    }
}
